home *** CD-ROM | disk | FTP | other *** search
/ PC User 2003 January / Disc 3 / Amethyst.iso / live / usr / lib / LST / group.pl next >
Encoding:
Perl Script  |  2002-10-23  |  2.1 KB  |  84 lines

  1. #!/usr/bin/perl -w
  2. # $Id: group.pl,v 1.3 1999/08/02 15:06:00 ray Exp $
  3. #
  4. # Adds a group to /etc/group, using gid as the lowest acceptable id.
  5. # Special IDs are :sys: (start at 40), and :user: (start at 100).
  6. #
  7. # Usage: group --create name gid [member member ...]
  8. #
  9. # Copyright (C) 1999 Caldera Systems, Inc.
  10.  
  11. $file      = "/etc/group";
  12. $minsysgid = 40;
  13. $minusrgid = 100;
  14.  
  15. $command = shift;
  16.  
  17. # For debugging
  18. if ($command =~ /-F(.*)/) {
  19.   $file = $1;
  20.   $command = shift;
  21. }
  22.  
  23. if ($command =~ /(--)?create/ ) {
  24.   $Is = $Iu = $i = 0;
  25.   $name = shift || die( "Error: group-name missing\n");
  26.   $gid  = shift || die( "Error: group-id missing\n");
  27.   $mem  = join(',',@ARGV);
  28.   
  29.   open GIN,"<$file";
  30.   #@L = <GIN>;
  31.   while ( <GIN> ) {
  32.     push( @L, $_);
  33.     if ( /^([^:]+):[^:]*:([0-9]+):/ ) {
  34.       $N{$2} = $1;
  35.       $G{$1} = $2;
  36.       $Is = $#L if ( $2 == $minsysgid );
  37.       $Iu = $#L if ( $2 == $minusrgid );
  38.     }
  39.   }
  40.   close( GIN);
  41.  
  42.   if ( exists( $G{$name}) ) {
  43.     print STDERR "Warning: group '$name' exists (keeping '$G{$name}').\n"
  44.       unless ( $G{$name} eq $gid );
  45.     exit( 0);
  46.    } elsif (  exists( $N{$gid}) ) {
  47.     print STDERR "Error: group-id '$gid' already used by '$N{$gid}'.\n";
  48.     exit( 1);
  49.   } elsif ($gid eq ":sys:") {
  50.     $gid  = $minsysgid;
  51.   } elsif ($gid eq ":user:") {
  52.     $gid  = $minusrgid;
  53.   } elsif ( $gid !~ /^[1-9][0-9]*$/ ) {
  54.     print STDERR "Error: Invalid group-id '$gid'.\n";
  55.     exit( 1);
  56.   } 
  57.   # Group not listed--here we go!
  58.   $i = $Is if ( $gid >= $minsysgid );
  59.   $i = $Iu if ( $gid >= $minusrgid );
  60.   while ( exists( $N{$gid}) ) {
  61.     $gid++;
  62.   }
  63.   if ( $gid > ((exists($G{nobody}))?$G{nobody}:65534) ) {
  64.     print STDERR "Error: group-id overflow!\n";
  65.     exit( 1);
  66.   }
  67.   for ( ; $i <= $#L; $i++) {
  68.     last if ($L[$i] =~ /^\+/);
  69.     next unless ($L[$i] =~ /^[^:]+:[^:]*:([0-9]+):/);   
  70.     last if ( $1 > $gid );
  71.   }
  72.   splice(@L,$i,0,"${name}::$gid:$mem\n");
  73.   open GOUT, ">$file.new" || die;
  74.   print GOUT @L;
  75.   rename("$file.new", "$file") || die;
  76.   0;
  77. } elsif ($command =~ /(--)?revoke/ ) {
  78.   print STDERR "Info: group revocation (currently?) not supported!\n";
  79.   0;
  80. } else {
  81.   print STDERR "Error: Unknown command '$command'.\n";
  82.   1;
  83. }
  84.